package com.etone.smartAudit.service;

import cn.hutool.core.collection.CollectionUtil;

import com.etone.smartAudit.commService.CommPUumOrganizationinfoService;
import com.etone.smartAudit.domain.PLogVersion;
import com.etone.smartAudit.domain.PUumOrganizationinfo;

import com.etone.smartAudit.vo.PortalBaseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import java.util.List;


/**
 * @author jiayen
 * @since 2021-12-07 11:58:05
 */
@Slf4j
@Service
public class PUumOrganizationinfoService {


    @Autowired
    private CommPUumOrganizationinfoService  commPUumOrganizationinfoService;
    @Autowired
    private PLogVersionService pLogVersionService;

    @Autowired
    private UDSService udsService;

    @Value("${uds.level:2}")
    private Integer ouLevel;

    /***
     * 同步组织架构
     */
    public void synOrgnization() throws Exception {
        PLogVersion lastLog = pLogVersionService.getOrgnizationLog();
        if (lastLog != null) {
            String logId = lastLog.getLogId();
            //获取异动组织数据
            PortalBaseVo<PUumOrganizationinfo> vo = udsService.bizQueryOrgChgHistory(logId);
            List<PUumOrganizationinfo> list = vo.getList();
            pLogVersionService.updatePersonLog(Boolean.valueOf(vo.getContinuFlag()), PLogVersionService.ORG_TYPE);
            if (CollectionUtil.isNotEmpty(list)) {
                list.stream().forEach(org -> {
                    commPUumOrganizationinfoService.addOrganization(org);
                });
            }
        }
    }

}
